package com.example.hrandroidapp;

import android.app.ActionBar;
import android.app.ActionBar.TabListener;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;

import com.example.hrandroidapp.adapter.TabsPagerAdapter;

public class LeaveApprovalActivity extends FragmentActivity implements TabListener {

	private ViewPager mViewPager;
	private TabsPagerAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = { "Pending", "Approve", "Reject", "MyLeave" };
	
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.leave_approval);
    	
    	loadViews();
    	
    	mViewPager.setAdapter(mAdapter);
    	actionBar.setHomeButtonEnabled(true);
    	actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    	
    	for (String tab_name : tabs) {
            actionBar.addTab(actionBar.newTab().setText(tab_name)
                    .setTabListener(this));
        }
        
    	mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        	 
             @Override
             public void onPageSelected(int position) {
                 // on changing the page
                 // make respected tab selected
                 actionBar.setSelectedNavigationItem(position);
             }
  
             @Override
             public void onPageScrolled(int arg0, float arg1, int arg2) {}
  
             @Override
             public void onPageScrollStateChanged(int arg0) {}
         });
    }
    
    private void loadViews() {
    	mViewPager = (ViewPager)findViewById(R.id.pager);
    	actionBar = getActionBar();
    	mAdapter = new TabsPagerAdapter(getSupportFragmentManager());
    }
    
    @Override
    public void onBackPressed() {
    	super.onBackPressed();
    	overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
    }

    @Override
    public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

    @Override
    public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
        mViewPager.setCurrentItem(tab.getPosition());
    }

    @Override
    public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	getMenuInflater().inflate(R.menu.leave_menu, menu);
		return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	int id = item.getItemId();
		
		if(id == R.id.action_leave) {
			Intent intent = new Intent(getApplicationContext(), CreateLeaveActivity.class);
			startActivity(intent); 
			overridePendingTransition(R.anim.pull_in_left, R.anim.push_out_right);
		}else if(id == android.R.id.home) {
			onBackPressed();
		}
		
		return super.onOptionsItemSelected(item);
    }

}
